/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-09-28 18:26:49
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-09-28 19:48:02
 * @FilePath: /Learn/test/thread.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma

#include<iostream>
#include<string>
#include<ctime>
#include<pthread.h>
#include<functional>
typedef void(*callback_t)(void*);
static int num=1;

class Thread
{
public:
    static void*Routine(void*args)
    {
        Thread *thread=static_cast<Thread*>(args);
        thread->Entery();
        return nullptr;
    }
public:
    Thread()
        :_tid(0)
        ,_start_timestamp(0)
        ,_name("")
        ,_isrunning(0)
        ,_args(nullptr)
    {}
    Thread(callback_t cb,void*args)
        :_tid(0)
        ,_start_timestamp(0)
        ,_name("")
        ,_isrunning(0)
        ,_cb(cb)
        ,_args(args)
    {}

    void Run()
    {
        _name="Thread "+std::to_string(num++);
        _start_timestamp=time(nullptr);
        _isrunning=1;
        pthread_create(&_tid,nullptr,Routine,this);
    }
    void Join()
    {
        pthread_join(_tid,nullptr);
        _isrunning=0;
    }
    std::string Name()
    {
        return _name;
    }
    uint64_t StartTimestamp()
    {
        return _start_timestamp;
    }
    bool isRunning()
    {
        return _isrunning;
    }
    //可以设置模板对象向里面传参
    void Entery()
    {
        _cb(_args);
    }

    pthread_t Tid()
    {
        return _tid;
    }
    ~Thread()
    {}
private:
    pthread_t _tid;//由pthread_create生成
    std::string _name;
    uint64_t _start_timestamp;//时间戳
    bool _isrunning;
    callback_t _cb;//函数指针
    void* _args;//回调函数
    //T t;
};